package com.yunhe.abnormal.service.data.impl;

import com.yunhe.abnormal.domain.data.AbnormalEventRecord;
import com.yunhe.abnormal.domain.data.AbnormalEventRecordMeas;
import com.yunhe.common.model.PageParam;
import com.yunhe.abnormal.repository.data.AbnormalEventRecordMeasRepository;
import com.yunhe.abnormal.service.data.AbnormalEventRecordMeasService;
import com.yunhe.abnormal.service.data.AbnormalEventRecordService;
import com.yunhe.common.util.QueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class AbnormalEventRecordMeasServiceImpl implements AbnormalEventRecordMeasService {

    @Autowired
    private AbnormalEventRecordMeasRepository abnormalEventRecordMeasRepository;
    @Autowired
    private AbnormalEventRecordService abnormalEventRecordService;

    @Override
    public AbnormalEventRecordMeas create(AbnormalEventRecordMeas abnormalEventRecordMeas) {
        abnormalEventRecordMeas.setId(null);
        AbnormalEventRecordMeas result = abnormalEventRecordMeasRepository.save(abnormalEventRecordMeas);
        return result;
    }

    @Override
    public Iterable<AbnormalEventRecordMeas> create(List<AbnormalEventRecordMeas> abnormalEventRecordMeasList) {
        List<AbnormalEventRecordMeas> results = new ArrayList<>();
        for (AbnormalEventRecordMeas abnormalEventRecordMeas : abnormalEventRecordMeasList) {
            results.add(create(abnormalEventRecordMeas));
        }
        return results;
    }

    @Override
    public void delete(Long id) {
        abnormalEventRecordMeasRepository.deleteById(id);
    }

    @Override
    public AbnormalEventRecordMeas update(AbnormalEventRecordMeas abnormalEventRecordMeas) {
        return null;
    }

    @Override
    public AbnormalEventRecordMeas update(Long id, Optional<Long> recordId, Optional<Long> pointNumber, Optional<Double> value) {
        return null;
    }

    @Override
    public Iterable<AbnormalEventRecordMeas> retrieve(Optional<Long> recordId,
                                                      Optional<List<Long>> pointNumber,
                                                      PageParam pageParam) {
        Specification spec = (Specification<AbnormalEventRecord>) (root, query, cb) -> {
            List<Predicate> criteria = new ArrayList<>();
            if (pointNumber.isPresent()) {
                CriteriaBuilder.In<Long> in = cb.in(root.get("pointNumber"));
                for (Long pn : pointNumber.get()) {
                    in.value(pn);
                }
                criteria.add(in);
            }
            if (recordId.isPresent()) {
                AbnormalEventRecord byId = abnormalEventRecordService.findById(recordId.get());
                if (byId != null) {
                    criteria.add(cb.equal(root.get("abnormalEventRecord").as(AbnormalEventRecord.class), byId));
                }
            }
            if (criteria.size() == 1) {
                query.where(criteria.get(0));
            } else {
                query.where(criteria.toArray(new Predicate[0]));
            }
            return null;
        };
        Iterable<AbnormalEventRecordMeas> results;
        if (QueryUtil.needPaging(pageParam)) {
            List<String> sortList = pageParam.getSort();
            if (sortList == null) {
                List<String> list = new ArrayList<>();
                list.add("-id");
                pageParam.setSort(list);
            }
            results = abnormalEventRecordMeasRepository.findAll(spec, QueryUtil.getPageRequest(pageParam));
        } else {
            Sort sort = pageParam == null ? null : QueryUtil.getSort(pageParam.getSort());
            if (sort != null) {
                results = abnormalEventRecordMeasRepository.findAll(spec, sort);
            } else {
                List<String> list = new ArrayList<>();
                list.add("-id");
                sort = QueryUtil.getSort(list);
                results = abnormalEventRecordMeasRepository.findAll(spec, sort);
            }
        }
        return results;
    }
}
